package com.qs.controller;


import com.qs.annotation.OperationLog;
import com.qs.constant.RedisConstant;
import com.qs.pojo.dto.NoticeDto;
import com.qs.pojo.dto.UserDto;
import com.qs.pojo.entity.Notice;
import com.qs.pojo.vo.NoticeVo;
import com.qs.response.Response;
import com.qs.service.INoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前后台公告表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-12-16
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final INoticeService noticeService;

    private final RedisTemplate redisTemplate;

    /**
     * 分页获取公告数据
     * @param pageNo
     * @param pageSize
     * @param noticeVo
     * @return
     */
    @GetMapping("/list/{pageNo}/{pageSize}")
    public Response list(@PathVariable("pageNo") Integer pageNo,
                         @PathVariable("pageSize") Integer pageSize,
                         NoticeVo noticeVo) {
        log.info("分页获取公告数据请求进来------------");
        Map<String, Object> map = noticeService.getNoticeList(pageNo,pageSize,noticeVo);
        return Response.success(map);
    }

    /**
     * 获取前台公告
     * @return
     */
    @GetMapping("/front/list")
    public Response frontList() {
        log.info("获取前台公告请求进来------------");
        List<NoticeDto> noticeDto = noticeService.getNoticeFrontList();
        return Response.success(noticeDto);
    }

    /**
     * 保存公告
     * @param notice
     * @return
     */
    @PostMapping("/save")
    @OperationLog(operModul = "公告管理-新增公告",operDesc = "管理员新增公告")
    public Response saveNotice(@RequestBody Notice notice) {
        log.info("保存公告请求进来------------");
        noticeService.saveNotice(notice);
        return Response.success();
    }

    /**
     * 修改公告
     * @param
     * @param
     * @param
     * @return
     */
    @PutMapping("/update")
    @OperationLog(operModul = "公告管理-修改公告",operDesc = "管理员修改公告")
    public Response update(@RequestBody Notice notice) {
        log.info("修改公告请求进来--------------------");
        noticeService.updateNotice(notice);
        return Response.success();
    }

    /**
     * 公告数据回显
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/getById/{id}")
    public Response echo(@PathVariable Integer id) {
        log.info("公告数据回显请求进来--------------------");
        Notice notice = noticeService.echo(id);
        return Response.success(notice);
    }

    /**
     * 删除公告
     * @param
     * @param
     * @param
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    @OperationLog(operModul = "公告管理-删除公告",operDesc = "管理员删除公告")
    public Response delete(@PathVariable List<Integer> ids) {
        log.info("删除普通用户请求进来--------------------");
        noticeService.removeBatchByIds(ids);
        redisTemplate.delete(RedisConstant.INDEX_NOTICE);
        return Response.success();
    }
}
